﻿using System;
using System.Web.Mvc;
using IterationLife.Extensions.MvcApi;
using IterationLife.Mobile.AppServices.Services.Common;
using Util;
using Util.Exceptions;

namespace IterationLife.Mobile.ApiGateway.Areas.Common.Controllers
{
    /// <summary>
    /// 内容资源评论控制器
    /// </summary>
    public class ContentResourceCommentController : MvcApiController
    {

        /// <summary>
        /// 内容资源评论服务
        /// </summary>
        private readonly IContentResourceCommentService _newsCommentService;

        /// <summary>
        /// 初始化内容资源评论控制器
        /// </summary>
        /// <param name="newsCommentService">内容资源评论服务</param>
        public ContentResourceCommentController(IContentResourceCommentService newsCommentService)
        {
            _newsCommentService = newsCommentService;
        }

        /// <summary>
        /// 评论列表
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="page">页数</param>
        /// <param name="size">条数</param>
        [HttpGet]
        public ActionResult PagedList(Guid id, int? page = 1, int? size = 5)
        {
            if (id.IsEmpty())
                throw new Warning("编号无效");
            if (page < 1)
                page = 1;
            if (size < 1)
                size = 5;
            var result = _newsCommentService.PagedList(id, page.SafeValue(), size.SafeValue());
            return Ok(result, "评论列表");
        }

        /// <summary>
        /// 评论
        /// </summary>
        /// <param name="id">内容资源编号</param>
        /// <param name="content">内容</param>
        [HttpPost]
        public ActionResult Add(Guid id, string content)
        {
            if (id.IsEmpty())
                throw new Warning("编号无效");
            if (content.IsEmpty())
                throw new Warning("内容不能为空");
            _newsCommentService.Add(id, content);
            return Ok(message: "评论成功");
        }
    }
}